/////////////////////////////////////////////////////////////////////////////
// Name:        choiceChart.cpp
// Purpose:     
// Author:      Herry Sugiharto
// Modified by: 
// Created:     24/07/2009 22:19:36
// RCS-ID:      
// Copyright:   Copyright (c) 2000, 2008 Krida Software Division
// Licence:     
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
////@end includes

#include "slposapp.h"
#include "choiceChart.h"

////@begin XPM images
////@end XPM images


/*!
 * choiceChart type definition
 */

IMPLEMENT_DYNAMIC_CLASS( choiceChart, wxChoice )


/*!
 * choiceChart event table definition
 */

BEGIN_EVENT_TABLE( choiceChart, wxChoice )

////@begin choiceChart event table entries
////@end choiceChart event table entries

END_EVENT_TABLE()


/*!
 * choiceChart constructors
 */

choiceChart::choiceChart()
{
    Init();
}

choiceChart::choiceChart(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, const wxArrayString& choices, long style, const wxValidator& validator)
{
    Init();
    Create(parent, id, pos, size, choices, style, validator);
}


/*!
 * choiceChart creator
 */

bool choiceChart::Create(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, const wxArrayString& choices, long style, const wxValidator& validator)
{
    wxChoice::Create(parent, id, pos, size, choices, style, validator);
    
    // add items to the control
    wxString strqry;
    strqry = wxT("SELECT id || ' ' || accno || '--' || description FROM chart WHERE charttype = 'A' ORDER BY accno");
    
    // get rows from table
    PGresult *res;
    
    res = PQexec( wxGetApp().conn, strqry.char_str() );
    if (PQresultStatus(res) != PGRES_TUPLES_OK)
    {
        //wxMessageBox(_("Unable to get record from table"));
        PQclear(res);
        return false;
    }
    
    // next, add the rows to choice control
    wxString Chart;
    int counter = 0;
    while ( counter <= PQntuples(res)-1 )
    {
        Chart.Empty();
        Chart = wxT(PQgetvalue(res, counter, 0));
        Append(Chart);
        counter++;
    };
    
    // clear the result and others to avoid memory leak
    PQclear(res);
    strqry.Clear();
    
    return true;
}


/*!
 * choiceChart destructor
 */

choiceChart::~choiceChart()
{
////@begin choiceChart destruction
////@end choiceChart destruction
}


wxString choiceChart::GetSelected()
{
    wxString Selected;
    Selected = GetStringSelection();
    Selected = Selected.Left(5);
    return Selected;
}


bool choiceChart::SetSelected(wxString& ID)
{
    // add items to the control
    wxString strqry;
    strqry = wxT("SELECT id || ' ' || accno || '--' || description FROM chart WHERE id = ") + ID;
    
    // get rows from table
    PGresult *res;
    
    res = PQexec( wxGetApp().conn, strqry.char_str() );
    if (PQresultStatus(res) != PGRES_TUPLES_OK)
    {
        //wxMessageBox(_("Unable to get record from table"));
        PQclear(res);
        return false;
    };
    
    // next, add the rows to choice control
    if ( PQntuples(res) >= 1 )
    {
        wxString Chart;
        Chart = wxT(PQgetvalue(res, 0, 0));
        SetStringSelection(Chart);
    };
    
    // clear the result and others to avoid memory leak
    PQclear(res);
    strqry.Clear();
    
    return true;
}
